let qCloud = require("../../index.js");
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    sizeListArr: {
      type: "array",
      value: []
    },
    showtype: {
      type: 'int',
      value: 1
    },
    showtitle: {
      type: 'string',
      value: 1
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgPath: qCloud.config.STATIC_IMAGE_PATH,
    spectypeid: 0,
    spectypetxt: '', //尺码类型显示文字
    specEmpty: {
      id: 1,
      color: '',
      spectypeid: 0,
      spectypetxt: '',
      spectypeindex: -1
    },
    sizeEmpty: {
      id: 0,
      size: '',
      num: '',
      price: '',
      money: ''
    },
    sizeTypeList: [{
        id: 1,
        title: '类型1',
        sizeList: [{
            id: 2,
            size: 'S',
            num: '200',
            price: 50,
            money: 100
          },
          {
            id: 3,
            size: 'M',
            num: '200',
            price: 50,
            money: 100
          },
          {
            id: 4,
            size: 'L',
            num: '200',
            price: 50,
            money: 100
          },
          {
            id: 5,
            size: 'XL',
            num: '200',
            price: 50,
            money: 100
          },
          {
            id: 6,
            size: 'XXL',
            num: '200',
            price: 50,
            money: 100
          }
        ]
      },
      {
        id: 2,
        title: '类型2',
        sizeList: [{
            id: 2,
            size: '30',
            num: '200',
            price: 50,
            money: 100
          },
          {
            id: 3,
            size: '32',
            num: '200',
            price: 50,
            money: 100
          },
          {
            id: 4,
            size: '36',
            num: '200',
            price: 50,
            money: 100
          },
          {
            id: 5,
            size: '38',
            num: '200',
            price: 50,
            money: 100
          }
        ]
      }
    ],
    specArray: [{
      id: 1,
      color: '',
      spectypeid: 0,
      spectypetxt: '',
      spectypeindex: -1
    }]
  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {

    },
    hide: function () {},
    resize: function () {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    addSpecTap(e) {
      let self = this
      let specArray = self.data.specArray
      let specEmpty = self.data.specEmpty;
      specEmpty.sizeTypeList = self.data.sizeTypeList
      specArray.push(self.data.specEmpty);
      self.setData({
        specArray: specArray
      })
    },
    delSpecTap(e) {
      let self = this
      let index = e.currentTarget.dataset.index;
      let specArray = self.data.specArray
      specArray.splice(index, 1);
      if (specArray.length == 0) {
        wx.showToast({
          title: '不能全部删除',
          icon: 'none'
        })
        return false;
      }
      self.setData({
        specArray: specArray
      })
    },
    delsizeTap(e) {
      let self = this
      let specindex = e.currentTarget.dataset.specindex;
      let sizeindex = e.currentTarget.dataset.sizeindex;
      let specArray = self.data.specArray
      let spectypeindex = specArray[specindex]["spectypeindex"];
      specArray[specindex]["sizeTypeList"][spectypeindex]["sizeList"].splice(sizeindex, 1);
      self.setData({
        specArray: specArray
      })
    },
    addsizeTap(e) {
      let self = this
      let index = e.currentTarget.dataset.index
      let specArray = self.data.specArray
      let spectypeindex = specArray[index]["spectypeindex"];
      specArray[index]["sizeTypeList"][spectypeindex]["sizeList"].push(self.data.sizeEmpty);
      self.setData({
        specArray: specArray
      })
    },
    colorinputtap(e) {
      let self = this
      let index = e.currentTarget.dataset.index
      let value = e.detail.value
      let specArray = self.data.specArray
      specArray[index]["color"] = value
      self.setData({
        specArray: specArray
      })
    },
    sizenum(e) {
      let self = this
      let sizeindex = e.currentTarget.dataset.sizeindex
      let specindex = e.currentTarget.dataset.specindex
      let specarray = self.data.specArray
      let currentspec = specarray[specindex]
      let spectypeindex = currentspec.spectypeindex
      let value = e.detail.value
      specarray[specindex]["sizeTypeList"][spectypeindex]["sizeList"][sizeindex]["num"] = value
      self.setData({
        specArray: specarray
      })

      self.setnum();
    },
    bindSizeTypeChange(e) {
      let self = this
      let specIndex = e.detail.value
      let sizeTypeList = self.data.sizeTypeList;
      let currentIndex = e.currentTarget.dataset.index
      let currentspec = sizeTypeList[specIndex];
      let specarray = self.data.specArray
      specarray[currentIndex].spectypeid = sizeTypeList[specIndex].id;
      specarray[currentIndex].spectypetxt = sizeTypeList[specIndex].title;
      specarray[currentIndex].spectypeindex = specIndex;
      self.setData({
        specArray: specarray
      })
    },
    setnum() {
      let self = this
      let specarray = self.data.specArray
      let nums = 0;
      for (var i = 0; i < specarray.length; i++) {
        let spectypeindex = specarray[i]["spectypeindex"];
        if (spectypeindex == -1) {
          continue;
        }
        let speclist = specarray[i]["sizeTypeList"][spectypeindex]["sizeList"];
        for (var j = 0; j < speclist.length; j++) {
          if (!isNaN(speclist[j]["num"])) {
            nums = nums * 1 + speclist[j]["num"] * 1;
          }
        }
      }
      this.triggerEvent('sizenuminput')
    },
    onLoad: function () {
      this.data.paramA // 页面参数 paramA 的值
      this.data.paramB // 页面参数 paramB 的值
    }
  },

  ready() {
    /*   let self = this
       let specArray = self.data.specArray
       for (var i = 0; i < specArray.length; i++) {
          specArray[i].sizeTypeList = self.data.sizeTypeList;
       }
       var specEmpty = self.data.specEmpty
       specEmpty.sizeTypeList = self.data.sizeTypeList
       self.setData({
          specArray: specArray,
          specEmpty: specEmpty
       }) */
  }
})
